package yxk.task;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人事业务档案赋值 年龄阶段，最高学历，第一学历字段
 */
public class HspmErmanfileTask extends AbstractTask implements Plugin {
    private static final Log LOG = LogFactory.getLog(HspmErmanfileTask.class);
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        LOG.info("=======进入=======");
        //查询人员事业档案
        QFilter qFilter=new QFilter("iscurrentversion", QCP.equals,'1')
                .and("datastatus",QCP.equals,'1')
                .and("businessstatus", QCP.equals,'1');
        DynamicObject[] loaded = BusinessDataServiceHelper.load("hspm_ermanfile", "person,pernontsprop,nwg5_age,nwg5_educationgree,nwg5_degree", qFilter.toArray());

        LOG.info("=======获取人员工号=======");
        List<String> numberList = new ArrayList<>();
        for (DynamicObject row : loaded) {
            DynamicObject person = row.getDynamicObject("person");
            String number = person.getString("number");
            numberList.add(number);
        }
        QFilter peredQF=new QFilter("iscurrentversion", QCP.equals,'1')
                .and("datastatus", QCP.equals,'1');
        LOG.info("=======进入学历过滤阶段=======");
        //学历
        Map<String,Map<String,String>> mapGree=new HashMap<>();
        if(numberList != null && numberList.size() > 0) {
            peredQF.and("person.number",QCP.in,numberList);
            DynamicObject[] pereduexps = BusinessDataServiceHelper.load("hrpi_pereduexp", "person,education,ishighestdegree,nwg5_isfirstdegree", peredQF.toArray());
            for (DynamicObject per : pereduexps) {
                //人员工号
                DynamicObject person = per.getDynamicObject("person");
                String number = person.getString("number");
                Map<String, String> greeMap = mapGree.get(number);
                if(greeMap == null){
                    greeMap=new HashMap<>();
                }
                //获取学历
                DynamicObject education = per.getDynamicObject("education");
                if(education == null){
                    continue;
                }
                String educationID = education.getPkValue().toString();


                //是否最高学历
                String ishighestdegree = per.getString("ishighestdegree");
                if("1".equals(ishighestdegree)){
                    greeMap.put("ationgree",educationID);
                }
                //是否第一学历
                String degree = per.getString("nwg5_isfirstdegree");
                if("1".equals(degree)){
                    greeMap.put("degree",educationID);
                }

                mapGree.put(number,greeMap);
            }
            LOG.info("=======进入赋值滤阶段=======");
            List<DynamicObject> ermanFileList=new ArrayList<>();
            for (DynamicObject row : loaded) {
                DynamicObject person = row.getDynamicObject("person");
                String number = person.getString("number");

                Map<String, String> degreeMap = mapGree.get(number);
                if(degreeMap != null && degreeMap.size() > 0){
                    //最高学历ID
                    String ationgree = degreeMap.get("ationgree");
                    //第一学历ID
                    String degree = degreeMap.get("degree");
                    row.set("nwg5_educationgree",degree);
                    row.set("nwg5_degree",ationgree);
                }
                //获取非时序属性
                DynamicObject pernontsprop = row.getDynamicObject("pernontsprop");
                if(pernontsprop != null){
                    int age = pernontsprop.getInt("age");
                    String stAge = getStAge(age);
                    row.set("nwg5_age",stAge);
                }
                //获取年龄
                ermanFileList.add(row);
            }

            DynamicObject[] rows=new DynamicObject[ermanFileList.size()];
            for (int i = 0; i < ermanFileList.size(); i++) {
                rows[i]=ermanFileList.get(i);
            }

            SaveServiceHelper.update(rows);
        }

    }

    private String getStAge(int age){
        String  txt="";
        if(age < 26){
            txt = "26";
        }else if(age >= 26 && age < 31){
            txt = "31";
        }else if(age >= 31 && age < 36){
            txt = "36";
        }else if(age >= 36 && age < 41){
            txt = "41";
        }else if(age >= 41 && age < 46){
            txt = "46";
        }else if(age >= 46 && age < 51){
            txt = "51";
        }else if(age >= 51 && age < 56){
            txt = "56";
        }else if(age >= 56){
            txt = "57";
        }
        return txt;
    }

}